#pragma once

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cassert>
#include <unistd.h>
#include <sys/types.h> // ftok mkfifo open
#include <sys/ipc.h> // shmget ftok
#include <sys/shm.h> // shmget
#include <sys/stat.h> // mkfifo open
#include <fcntl.h> // mkfifo open
#include "Log.hpp"

using namespace std;

#define PATH_NAME "/home/rtx2"
#define PROJ_ID 0x66 // 随便写的
#define SHM_SIZE 4096 //共享内存的大小，最好是页(PAGE: 4096)的整数倍，多1字节也会多创建一页

#define FIFO_NAME "./fifo"

class Init // 创建全局变量让它自己加载
{
public:
    Init()
    {
        umask(0);
        int n = mkfifo(FIFO_NAME, 0666);
        assert(n == 0);
        (void)n;
        Log("create fifo success",Notice) << "\n";
    }
    ~Init()
    {
        unlink(FIFO_NAME);
        Log("remove fifo success",Notice) << "\n";
    }
};

#define READ O_RDONLY
#define WRITE O_WRONLY

int OpenFIFO(std::string pathname, int flags)
{
    int fd = open(pathname.c_str(), flags);
    assert(fd >= 0);
    return fd;
}

void Wait(int fd)
{
    Log("等待中...", Notice) << "\n";
    uint32_t temp = 0;
    ssize_t s = read(fd, &temp, sizeof(uint32_t));
    assert(s == sizeof(uint32_t));
    (void)s;
}

void Signal(int fd) // 唤醒另一方过来读
{
    uint32_t temp = 1;
    ssize_t s = write(fd, &temp, sizeof(uint32_t));
    assert(s == sizeof(uint32_t));
    (void)s;
    Log("唤醒中...", Notice) << "\n";
}

void CloseFifo(int fd)
{
    close(fd);
}